package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.common.RedisConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.logging.Filter;

/**
* @Author : wangjie
* @CreateDate : 2020/4/6 16:02
* @Version : 1.0
* @Description : 会员前端控制器
*/
@RestController
@RequestMapping("mobile/member")
@Slf4j
public class MemberController {
    //依赖注入jedispool
    @Autowired
    private JedisPool jedisPool;
    //依赖注入service
    @Reference
    private MemberService memberService;

    @RequestMapping("smsLogin")
    public Result smsLogin(@RequestBody Map<String,String> map){
        try {
            //获取客户端发送的手机号和验证码
            String telephone = map.get("telephone");
            String validateCode = map.get("validateCode");
            //获取redis中的验证码
            Jedis jedis = jedisPool.getResource();
            String jedisCode = jedis.get(telephone + RedisConst.SENDTYPE_LOGIN);
            System.out.println("telephone="+telephone+", validataCode="+validateCode+", jedisCode="+jedisCode);
            //判断输入的和redis中的验证码是否一致
            //如果用户是5分钟后输入的验证码
            if (jedisCode==null){
                return new Result(false,"验证码失效");
            }
            //如果输入的验证码和redis中的验证码不一致
            if (!jedisCode.equals(validateCode)){
                return new Result(false,"验证码错误");
            }
            //调用service 方法获取登录
            memberService.smsLogin(telephone);
            //返回登录成功信息
            return new Result(true, MessageConst.LOGIN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回登录失败的信息
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

}
